#! /usr/bin/env python3
# -*-coding: utf-8-*-

__author__ = "Moonkie"


import numpy as np
import cv2


# self.traindata,self.pretest,self.mean = kl_transfrom(self.imagevects,'C',.8)
            # print(self.traindata.shape)
            # print(self.pretest.shape)
            # print(self.mean.shape)
            # r = get_rddata(self.traindata,self.pretest,self.mean)
            # img = r[:,1]
            # img = np.reshape(img,(112,92,3))
            # cv2.imshow('pic1',img)
            # cv2.waitKey(0)
            # cv2.destroyAllWindows()
def pca_compress(data,rate):
    '''PCA数据降维

    返回经过处理后的降维数据
    '''
    # print(data)
    mean = np.mean(data,axis=0)
    t = data - mean
    covs = np.cov(t.T)
    # covs = np.cov(t,rowvar=0)
    eigvals,eigvects = np.linalg.eig(covs)
    eigvalsb2s = np.argsort(eigvals)
    topn = int(len(eigvals) * rate + 1)
    eigvalsb2s = eigvalsb2s[:-topn:-1]
    rseigvects = eigvects[:,eigvalsb2s]
    nldata = np.dot(t,rseigvects)
    return nldata,rseigvects,mean   # 转换后的图像


def get_rddata(nldata,rdvects,mean):
    return np.uint8((np.dot(nldata,rdvects.T)) + mean)